﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace iShoppingDAO
{
    public class ChiTietPhanQuyenDAO
    {
        public static bool DeleteChiTietPhanQuyen(string maLoaiTaiKhoan, string maQuyen)
        {
            bool res = false;
            try
            {
                List<SqlParameter> sqlParameters = new List<SqlParameter>();
                string sql = "DELETE FROM ChiTietPhanQuyen WHERE MaLoaiTK = @MaLoaiTK AND MaPhanQuyen = @MaPhanQuyen";
                SqlParameter[] parameter = new SqlParameter[2];
                parameter[0] = new SqlParameter("@MaLoaiTK", SqlDbType.VarChar);
                parameter[0].Value = maLoaiTaiKhoan;
                sqlParameters.Add(parameter[0]);

                parameter[1] = new SqlParameter("@MaPhanQuyen", SqlDbType.VarChar);
                parameter[1].Value = maQuyen;
                sqlParameters.Add(parameter[1]);

                res = DataProvider.ExecuteNoneQuery(sql, sqlParameters);

            }
            catch (Exception ex)
            {
                res = false;
            }
            return res;
        }


        public static string SelectMaChiTiet(string maLoai, string maPhanQuyen)
        {
            string maChiTiet = null;
            try
            {

                string sql = "SELECT * FROM ChiTietPhanQuyen WHERE MaLoaiTK= '" + maLoai + "' AND MaPhanQuyen = '" + maPhanQuyen + "'" ;
                DataTable dt = DataProvider.ExecuteQuery(sql);
                if(dt.Rows.Count>0)
                {
                    maChiTiet = dt.Rows[0]["MaChiTietPhanQuyen"].ToString();
                }
                

            }
            catch (Exception ex)
            {
                maChiTiet = null;
            }
            return maChiTiet;
        }

        public static bool UpdateChiTietQuyen(int idChiTiet, string maQuyen)
        {
            bool res = false;
            try
            {
                List<SqlParameter> sqlParameters = new List<SqlParameter>();
                string sql = "UPDATE ChiTietPhanQuyen SET MaPhanQuyen = @MaPhanQuyen WHERE MaChiTietPhanQuyen = @MaChiTietPhanQuyen";
                SqlParameter[] parameter = new SqlParameter[2];

                parameter[0] = new SqlParameter("@MaPhanQuyen", SqlDbType.NVarChar);
                parameter[0].Value = maQuyen;
                sqlParameters.Add(parameter[0]);
                parameter[1] = new SqlParameter("@MaChiTietPhanQuyen", SqlDbType.VarChar);
                parameter[1].Value = idChiTiet;
                sqlParameters.Add(parameter[1]);

                res = DataProvider.ExecuteNoneQuery(sql, sqlParameters);

            }
            catch (Exception ex)
            {
                res = false;
            }
            return res;
        }


        public static bool InsertChiTiet(string maLoai, string maQuyen)
        {
            bool res = false;
            try
            {
                string sql = "INSERT INTO ChiTietPhanQuyen(MaLoaiTK,MaPhanQuyen) VALUES(@MaLoaiTK,@MaPhanQuyen)";
                List<SqlParameter> parameters = new List<SqlParameter>();

                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@MaLoaiTK", SqlDbType.NVarChar);
                par[0].Value = maLoai;
                parameters.Add(par[0]);
                par[1] = new SqlParameter("@MaPhanQuyen", SqlDbType.NVarChar);
                par[1].Value = maQuyen;
                parameters.Add(par[1]);

                res = DataProvider.ExecuteNoneQuery(sql, parameters);

            }
            catch (Exception)
            {
                res = false;
            }
            return res;
        }
    }
}
